package com.example.wastes.staff.controller;

import com.example.wastes.common.pojo.Admin;
import com.example.wastes.common.pojo.Staff;
import com.example.wastes.common.pojo.User;
import com.example.wastes.common.util.JwtUtils;
import com.example.wastes.common.util.R;
import com.example.wastes.staff.service.StaffService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class StaffController {
    @Autowired
    private StaffService staffService;
    @Autowired
    private JwtUtils jwtUtils;
    @GetMapping("/getAllStaff")
    @ApiOperation("查询所有员工信息")
    public R getAllStaff(){
        return new R(true,staffService.list());
    }
    @PostMapping("/saveStaff")
    @ApiOperation("添加员工信息")
    public R saveStaff(@RequestBody Staff staff){
        return new R(staffService.save(staff));
    }
    @PutMapping("/updateStaff")
    @ApiOperation("修改员工信息")
    public R updateStaff(@RequestBody Staff staff){
        return new R(staffService.updateById(staff));
    }
    @DeleteMapping("/deleteStaff")
    @ApiOperation("删除员工信息")
    public R deleteStaff(Integer id){
        return new R(staffService.removeById(id));
    }
    //模糊查询
    @GetMapping("/findStaffName")
    @ApiOperation("通过名字查询")
    public R findStaffName(String username){
        return new R(true,staffService.findStaffName(username));
    }

    @GetMapping("/login")
    public Object tologin(@RequestParam(value = "username") String username,@RequestParam(value = "password") String password){
        long userId=0;
        Map<String, Object> map = new HashMap<>();
        if(username == null || password == null){
            map.put("code",100);
            map.put("msg","请求参数不能为空");
        }else {
            Staff staff = staffService.tologin(username);
            if (staff !=null){
                if (password.equals(staff.getPassword())){
                    userId = 1;
                    String token= jwtUtils.generateToken(userId);
                    map.put("code",200);
                    map.put("msg","登陆成功");
                    map.put("token",token);
                    map.put("user",staff);
                }else {
                    map.put("code",103);
                    map.put("msg","用户名和密码错误");
                }
            }else{
                map.put("code",102);
                map.put("msg","该用户不存在");
            }
        }
        return map;
    }

    @GetMapping("/register")
    public String register(String username,String password){
//        System.out.println(username);
//        System.out.println(password);
        if (StringUtils.isEmpty(username)){
            return "用户名不允许为空";
        }
        if (StringUtils.isEmpty(password)){
            return "密码不允许为空";
        }
        Staff staff = staffService.tologin(username);
        if (staff != null){
            return "注册失败，用户存在";
        }
        int resultCount = staffService.saveStaff(username, password);
        System.out.println(resultCount);
        if (resultCount == 0) {
            return "注册失败";
        }
        return "注册成功";
    }

    @PostMapping("/upload2")
    //上传头像
    public String upload2(Staff staff, MultipartFile filename, HttpServletRequest request){
        int i = staffService.upload2(staff,filename,request);
        if (i>0){
            return "上传成功";
        }else{
            return "上传失败";
        }
    }
}
